package ru.kantiana.accounting.rldata.core.bazaok.data;

import java.util.ArrayList;
import java.util.Collection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author: Pavel Belevich
 * Date: 4/2/11
 * Time: 9:17 PM
 */
public class BOParserImpl implements BOParser {

    private static final String ENTRY_PATTERN_STRING = "тн\\s*:\\s*([\\S&&[^;]]*?)\\s*;.+?(фио\\s*:\\s*(.*?)\\s*;).+?(?=тн\\s*:|\\Z)";

    private static final Pattern ENTRY_PATTERN = Pattern.compile(ENTRY_PATTERN_STRING, Pattern.DOTALL);

    public Collection<BOEntry> parse(CharSequence charSequence) {
        Collection<BOEntry> BOEntries = new ArrayList<BOEntry>();

        Matcher matcher = ENTRY_PATTERN.matcher(charSequence);

        while (matcher.find()) {
            BOEntry boEntry = new BOEntry();
            boEntry.setText(matcher.group(0));
            boEntry.setNumber(matcher.group(1));
            boEntry.setName(matcher.group(3));
            BOEntries.add(boEntry);
        }

        return BOEntries;
    }
}
